package com.dingreading.cloud.mall.util;


import com.dingreading.cloud.common.util.DoubleUtil;
import com.dingreading.cloud.common.util.EnumType;
import com.dingreading.cloud.common.util.NepUtil;
import com.dingreading.cloud.mall.dto.ShopCartPriceDto;
import com.dingreading.cloud.mall.entity.Goods;
import com.dingreading.cloud.mall.entity.GoodsType;
import com.dingreading.cloud.mall.entity.ShopCart;

public class ShopCartUtilV2 {


    /**
     * 新增正常商品的购物车记录
     */
    public static ShopCart newShopCart(
            Goods goods, GoodsType goodsType, String corpUid, String storeUid, String staffUid,
            Integer type, Integer mainComboType, String uid, Integer buyNum, String addUid, String addName,
            Integer freeDeliveryStatus, Double storeGoodsDiscountRate
    ) {
        ShopCart data = newBaseShopCart(
                goods, goodsType, corpUid, storeUid, staffUid, type, buyNum, uid, mainComboType, addUid, addName,
                freeDeliveryStatus, storeGoodsDiscountRate
        );

        data.setPaymentMethod(goods.getPaymentMethod());
        data.setTotalWeight(DoubleUtil.mul(buyNum, NepUtil.nullToZero(goodsType.getWeight())));
        return data;
    }

    /**
     * 新增组合商品的购物车记录
     */
    public static ShopCart newMainComboShopCart(
            Goods goods, GoodsType goodsType, String corpUid, String storeUid, String staffUid,
            Integer type, Integer mainComboType, String uid, Integer buyNum, String addUid, String addName,
            Integer paymentMethod, Double totalWeight,
            Integer freeDeliveryStatus, Double storeGoodsDiscountRate
    ) {
        ShopCart data = newBaseShopCart(
                goods, goodsType, corpUid, storeUid, staffUid, type, buyNum, uid, mainComboType, addUid, addName,
                freeDeliveryStatus, storeGoodsDiscountRate
        );

        data.setPaymentMethod(paymentMethod);
        data.setTotalWeight(totalWeight);
        return data;
    }

    /**
     * 新增组合子商品的购物车记录
     */
    public static ShopCart newChildComboShopCart(
            Goods goods, GoodsType goodsType, String corpUid, String storeUid, String staffUid,
            Integer type, Integer mainComboType, String uid, Integer buyNum, String addUid, String addName,
            String mainShopCartUid, Long mainGoodsId, String mainGoodsName, Long mainGoodsTypeId, String mainGoodsTypeName,
            Integer freeDeliveryStatus
    ) {
        ShopCart data = newBaseShopCart(
                goods, goodsType, corpUid, storeUid, staffUid, type, buyNum, uid, mainComboType,
                addUid, addName, freeDeliveryStatus, null
        );

        data.setPaymentMethod(goods.getPaymentMethod());
        double weight = NepUtil.nullToZero(goodsType.getWeight());
        data.setWeight(weight);
        data.setTotalWeight(DoubleUtil.mul(buyNum, weight));
        data.setMainShopCartUid(mainShopCartUid);
        data.setMainGoodsId(mainGoodsId);
        data.setMainGoodsTypeId(mainGoodsTypeId);
        data.setMainGoodsName(mainGoodsName);
        data.setMainGoodsTypeName(mainGoodsTypeName);
        return data;
    }

    /**
     * 新增购物车信息
     */
    private static ShopCart newBaseShopCart(
            Goods goods, GoodsType goodsType, String corpUid, String storeUid, String staffUid,
            Integer type, Integer buyNum, String uid, Integer mainComboType, String addUid, String addName,
            Integer freeDeliveryStatus, Double storeGoodsDiscountRate
    ) {
        ShopCart data = new ShopCart();
        data.setType(type);
        data.setCorpUid(corpUid);
        data.setStoreUid(storeUid);
        data.setStaffUid(staffUid);
        data.setGoodsId(goods.getId());
        data.setGoodsName(goods.getName());
        data.setImg(goodsType.getImg());
        data.setGoodsTypeId(goodsType.getId());
        data.setGoodsTypeName(goodsType.getName());
        data.setRealStatus(goods.getRealStatus());
        data.setUnit(goodsType.getUnit());
        data.setMinBuyNum(goods.getMinBuyNum());
        data.setBuyNum(buyNum);

        double unitPrice = goodsType.getPrice();
        data.setOriginalUnitPrice(unitPrice);
        double originalPrice = DoubleUtil.mul(buyNum, unitPrice);
        data.setOriginalPrice(originalPrice);

        ShopCartPriceDto priceDto = getDiscountRateUnitPrice(
                unitPrice, goodsType.getDiscountStatus(), storeGoodsDiscountRate
        );
        data.setUnitPrice(priceDto.getUnitPrice());
        double price = DoubleUtil.mul(buyNum, priceDto.getUnitPrice());
        data.setPrice(price);
        data.setDiscountRate(priceDto.getDiscountRate());
        data.setDiscountPrice(DoubleUtil.sub(originalPrice, price));
        data.setRemark(priceDto.getRemark());

        data.setCouponStatus(goods.getCouponStatus());
        data.setStatus(EnumType.ShopCart.WAIT_CONFIRM.getType());
        data.setStatusName(EnumType.ShopCart.WAIT_CONFIRM.getName());
        data.setStockOutMethod(goodsType.getStockOutMethod());
        data.setAddStaffUid(addUid);
        data.setAddStaffName(addName);
        data.setLastStaffUid(addUid);
        data.setLastStaffName(addName);
        data.setUid(uid);
        data.setComboType(goodsType.getComboType());
        data.setMainComboType(mainComboType);
        data.setWeight(NepUtil.nullToZero(goodsType.getWeight()));

        if (freeDeliveryStatus == null)
            data.setFreeDeliveryStatus(0);
        else
            data.setFreeDeliveryStatus(freeDeliveryStatus);
        return data;
    }

    /**
     * 更新购物车购买数量
     */
    public static void mdyShopCart(
            ShopCart data, Goods goods, GoodsType goodsType, Integer buyNum, String staffUid, String staffName,
            Double storeGoodsDiscountRate
    ) {
        data.setBuyNum(buyNum);
        double weight = NepUtil.nullToZero(goodsType.getWeight());
        data.setWeight(weight);
        data.setTotalWeight(DoubleUtil.mul(buyNum, weight));
        data.setPaymentMethod(goods.getPaymentMethod());

        double unitPrice = goodsType.getPrice();
        data.setOriginalUnitPrice(unitPrice);
        double originalPrice = DoubleUtil.mul(buyNum, unitPrice);
        data.setOriginalPrice(originalPrice);

        ShopCartPriceDto priceDto = getDiscountRateUnitPrice(
                unitPrice, goodsType.getDiscountStatus(), storeGoodsDiscountRate
        );
        data.setUnitPrice(priceDto.getUnitPrice());
        double price = DoubleUtil.mul(buyNum, priceDto.getUnitPrice());
        data.setPrice(price);
        data.setDiscountRate(priceDto.getDiscountRate());
        data.setDiscountPrice(DoubleUtil.sub(originalPrice, price));
        data.setRemark(priceDto.getRemark());

        data.setMinBuyNum(goods.getMinBuyNum());
        data.setCouponStatus(goods.getCouponStatus());
        data.setStockOutMethod(goodsType.getStockOutMethod());
        data.setLastStaffUid(staffUid);
        data.setLastStaffName(staffName);
    }

    /**
     * 更新购物车购买数量
     */
    public static void mdyMainComboShopCart(
            ShopCart data, Goods goods, GoodsType goodsType, Integer buyNum, Integer paymentMethod, Double totalWeight,
            String staffUid, String staffName, Double storeGoodsDiscountRate
    ) {
        data.setBuyNum(buyNum);
        data.setTotalWeight(totalWeight);
        data.setPaymentMethod(paymentMethod);

        double unitPrice = goodsType.getPrice();
        data.setOriginalUnitPrice(unitPrice);
        double originalPrice = DoubleUtil.mul(buyNum, unitPrice);
        data.setOriginalPrice(originalPrice);

        ShopCartPriceDto priceDto = getDiscountRateUnitPrice(
                unitPrice, goodsType.getDiscountStatus(), storeGoodsDiscountRate
        );
        data.setUnitPrice(priceDto.getUnitPrice());
        double price = DoubleUtil.mul(buyNum, priceDto.getUnitPrice());
        data.setPrice(price);
        data.setDiscountRate(priceDto.getDiscountRate());
        data.setDiscountPrice(DoubleUtil.sub(originalPrice, price));
        data.setRemark(priceDto.getRemark());

        data.setMinBuyNum(goods.getMinBuyNum());
        data.setCouponStatus(goods.getCouponStatus());
        data.setStockOutMethod(goodsType.getStockOutMethod());
        data.setLastStaffUid(staffUid);
        data.setLastStaffName(staffName);
    }

    /**
     * 计算购物车商品的单价（学科折扣）
     */
    public static ShopCartPriceDto getDiscountRateUnitPrice(
            Double unitPrice, Integer discountStatus, Double discountRate
    ) {
        ShopCartPriceDto priceDto = new ShopCartPriceDto();
        priceDto.setOriginalUnitPrice(unitPrice);
        priceDto.setUnitPrice(unitPrice);
        // 1.商品规格符合门店学科的折扣规则
        // 2.商品不在商品代金券范围内
        if (discountStatus != null && discountStatus.equals(1) && discountRate != null && discountRate > 0) {
            double discountRateUnitPrice = DoubleUtil.mul(unitPrice, discountRate);
            priceDto.setUnitPrice(Math.max(discountRateUnitPrice, 0.01));
            priceDto.setDiscountRate(discountRate);
            priceDto.setRemark("学科优惠折扣：" + discountRate);

        }

        return priceDto;
    }


}
